//
//  SMHomeHeadTableViewCell.m
//  ShoppingMall
//
//  Created by Demon on 2018/3/22.
//  Copyright © 2018年 Demon. All rights reserved.
//

#import "SMHomeHeadTableViewCell.h"
#import <SDWebImageManager.h>
#import <UIImage+GIF.h>
#import <SDCycleScrollView.h>

@interface SMHomeHeadTableViewCell () <SDCycleScrollViewDelegate>

@property (nonatomic, strong) NSArray *btnArray;
@property (nonatomic, strong) NSArray *labelArray;

@property (weak, nonatomic) IBOutlet UIButton *adsBtn;
@property (weak, nonatomic) IBOutlet SDCycleScrollView *lunboView;
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UILabel *lb1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UILabel *lb2;
@property (weak, nonatomic) IBOutlet UIButton *btn3;
@property (weak, nonatomic) IBOutlet UILabel *lb3;
@property (weak, nonatomic) IBOutlet UIButton *btn4;
@property (weak, nonatomic) IBOutlet UILabel *lb4;
@property (weak, nonatomic) IBOutlet UIButton *btn5;
@property (weak, nonatomic) IBOutlet UILabel *lb5;
@property (weak, nonatomic) IBOutlet UIButton *btn6;
@property (weak, nonatomic) IBOutlet UILabel *lb6;
@property (weak, nonatomic) IBOutlet UIButton *btn7;
@property (weak, nonatomic) IBOutlet UILabel *lb7;
@property (weak, nonatomic) IBOutlet UIButton *btn8;
@property (weak, nonatomic) IBOutlet UILabel *lb8;
@property (weak, nonatomic) IBOutlet UIButton *btn9;
@property (weak, nonatomic) IBOutlet UILabel *lb9;
@property (weak, nonatomic) IBOutlet UIButton *btn10;
@property (weak, nonatomic) IBOutlet UILabel *lb10;

@property (nonatomic, strong) SMHomeModel *homeModel;

@end

@implementation SMHomeHeadTableViewCell

- (void)setHeadCellModel:(SMHomeModel *)homeModel {
    self.homeModel = homeModel;
    NSArray *imgs = [homeModel.lunbo valueForKeyPath:@"imgurl"];
    self.lunboView.imageURLStringsGroup = imgs;
    SMAdsModel *adsModel = homeModel.ads[0];
    [[SDWebImageManager sharedManager] loadImageWithURL:[NSURL URLWithString:adsModel.imgurl] options:SDWebImageQueryDataWhenInMemory progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        UIImage *images = [UIImage sd_animatedGIFWithData:data];
        [self.adsBtn setBackgroundImage:images forState:UIControlStateNormal];
    }];
    
    for (int i = 0; i < homeModel.channel.count; i++) {
        SMChannelModel *model = homeModel.channel[i];
        UIButton *button = self.btnArray[i];
        UILabel *lb = self.labelArray[i];
        [button sd_setBackgroundImageWithURL:[NSURL URLWithString:model.imgurl] forState:UIControlStateNormal];
        lb.text = model.name;
    }
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.lunboView.delegate = self;
    self.lunboView.showPageControl = YES;
}

- (IBAction)adsBtnClick:(UIButton *)sender {
    SMAdsModel *adsModel = self.homeModel.ads[0];
    if (self.delegate && [self.delegate respondsToSelector:@selector(homeCellWithName:h5url:dataurl:)]) {
        [self.delegate homeCellWithName:adsModel.name h5url:adsModel.h5url dataurl:adsModel.dataurl];
    }
}

- (IBAction)channelBtnClick:(UIButton *)sender {
    SMChannelModel *channelModel = self.homeModel.channel[sender.tag - 1];
    if (self.delegate && [self.delegate respondsToSelector:@selector(homeCellWithName:h5url:dataurl:)]) {
        [self.delegate homeCellWithName:channelModel.name h5url:channelModel.h5url dataurl:channelModel.dataurl];
    }
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    SMLuoboModel *lunBoModel = self.homeModel.lunbo[index];
    if (self.delegate && [self.delegate respondsToSelector:@selector(homeCellWithName:h5url:dataurl:)]) {
        [self.delegate homeCellWithName:lunBoModel.name h5url:lunBoModel.h5url dataurl:lunBoModel.dataurl];
    }
}

- (NSArray *)btnArray {
    if (!_btnArray) {
        _btnArray = @[self.btn1,self.btn2,self.btn3,self.btn4,self.btn5,self.btn6,self.btn7,self.btn8,self.btn9,self.btn10];
    }
    return _btnArray;
}

- (NSArray *)labelArray {
    if (!_labelArray) {
        _labelArray = @[self.lb1,self.lb2,self.lb3,self.lb4,self.lb5,self.lb6,self.lb7,self.lb8,self.lb9,self.lb10];
    }
    return _labelArray;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end

